package com.osgix.demo.mvc.controller;

import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.osgix.common.mvc.AbsMultiActionController;
import com.osgix.common.utils.JsonUtils;
import com.osgix.common.widget.extTree.ExtTreeNode;

@Controller
@RequestMapping("/ajax.htm")
public class ajaxController extends AbsMultiActionController {
	private static final Logger logger = Logger.getLogger(ajaxController.class);

	@RequestMapping(params = "action=getJson")
	protected ModelAndView getJson(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Locale locale = RequestContextUtils.getLocale(request);
		Map<String, String> params = getAllParams(request);
		String jsonStr = JsonUtils.getJson(params);
		outPutJson(jsonStr, request, response);
		return null;
	}

	@RequestMapping(params = "action=getNode")
	protected ModelAndView getNode(HttpServletRequest request, HttpServletResponse response, ModelMap map)
			throws Exception {
		Locale locale = RequestContextUtils.getLocale(request);
		ExtTreeNode node = new ExtTreeNode();
		node.setId("1");
		node.setText("节点1");
		String jsonStr = JsonUtils.getJson(node);
		outPutJson(jsonStr, request, response);
		return null;
	}

	@RequestMapping(params = "action=getInfo")
	protected ModelAndView getInfo(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") String id, ModelMap model) throws Exception {
		Locale locale = RequestContextUtils.getLocale(request);
		ExtTreeNode node = new ExtTreeNode();
		node.setId(id);
		node.setText("节点1");
		String jsonStr = JsonUtils.getJson(node);
		// outPutJson(jsonStr, request, response);
		model.addAttribute("node", node);
		String jsonStr2 = JsonUtils.getJson(model);
		outPutJson(jsonStr2, request, response);
		return null;
	}

	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
	}
}
